package com.czk.model.strategy.resolvefile;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 实现ApplicationContextAware接口将实现类注入
 *
 * @Author:ChenZhangKun
 * @Date: 2021/10/27 21:31
 */
@Service
public class ResolveFile implements ApplicationContextAware {
    /**
     * 将策略保存在map中
     */
    private Map<String, IFileStrategy> map = new HashMap<>();

    /**
     * 解析
     *
     * @param fileType
     */
    public void resolve(String fileType) {
        IFileStrategy iFileStrategy = map.get(fileType);
        iFileStrategy.resolve("czk");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IFileStrategy> beansOfType = applicationContext.getBeansOfType(IFileStrategy.class);
        // 遍历
        beansOfType.values().forEach(type -> map.put(type.gainFileType(), type));
    }
}
